package spass.scene;

import spass.geometry.Point;
import spass.geometry.Ray;
import spass.geometry.Vector;
import spass.renderer.Color;
import spass.scene.forms.Form;
import spass.scene.forms.Intersection;
import spass.scene.light.DirectionalLight;

public class ColoredForm implements SceneObject {

    private final Form form;
    private final Color color;

    public ColoredForm(Form form, Color color) {
        this.form = form;
        this.color = color;
    }

    @Override
    public Color colorInDirectionalLightAt(Intersection intersection, DirectionalLight directionalLight) {
        Point intersectionPoint = intersection.getPoint();

        Vector perpendicularVector = form.surfaceNormalAt(intersectionPoint);
        Vector lightDirection = directionalLight.getDirection();

        double angle = lightDirection.angleBetween(perpendicularVector);
        double scalingFactor = -Math.cos(angle);
        double boundedScalingFactor = Math.max(scalingFactor, 0.0);

        return color.scale(boundedScalingFactor);
    }

    @Override
    public Intersection closestIntersection(Ray ray) {
        Point intersectionPoint = form.firstIntersectionPoint(ray);

        if (intersectionPoint != null) {
            return new Intersection(this, intersectionPoint);
        }

        return null;
    }

}
